package com.curvebreakers.wheresmystuff.activities;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import com.curvebreakers.wheresmystuff.R;
import com.curvebreakers.wheresmystuff.model.Category;
import com.curvebreakers.wheresmystuff.model.EntryType;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.content.Intent;
import android.view.*;
import android.widget.*;

public class EntrySortFragment extends Fragment {

	private Spinner categorySpinner, typeSpinner, dateSpinner;
	private EditText minRewardEdit, maxRewardEdit;
	private Spinner statusSpinner;
	private EditText searchEdit, zip_codeEdit;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		final View view = inflater.inflate(R.layout.fragment_entry_sort,
				container, false);
		categorySpinner = (Spinner) view.findViewById(R.id.categorySpinner);
		typeSpinner = (Spinner) view.findViewById(R.id.typeSpinner);
		dateSpinner = (Spinner) view.findViewById(R.id.dateSpinner);
		minRewardEdit = (EditText) view.findViewById(R.id.minReward);
		maxRewardEdit = (EditText) view.findViewById(R.id.maxReward);
		statusSpinner = (Spinner) view.findViewById(R.id.statusSpinner);
		searchEdit = (EditText) view.findViewById(R.id.search_filter);
		zip_codeEdit = (EditText) view.findViewById(R.id.zip_code_filter);

		view.findViewById(R.id.searchButton).setOnClickListener(
				new View.OnClickListener() {
					public void onClick(View v) {
						filter();
					}
				});

		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		ArrayAdapter<Category> catarray = new ArrayAdapter<Category>(
				getActivity(), android.R.layout.simple_spinner_dropdown_item,
				Category.values());
		ArrayAdapter<EntryType> typearray = new ArrayAdapter<EntryType>(
				getActivity(), android.R.layout.simple_spinner_dropdown_item,
				EntryType.values());

		categorySpinner.setAdapter(catarray);
		typeSpinner.setAdapter(typearray);
	}

	/**
	 * Called by the filter button. Redirects to a list of entries filtered by
	 * the parameters specified in the form.
	 * 
	 * @param v
	 */
	private void filter() {
		Intent changePage = new Intent(getActivity(), EntryListActivity.class);

		Category category = (Category) categorySpinner.getSelectedItem();
		EntryType type = (EntryType) typeSpinner.getSelectedItem();
		String choice = (String) dateSpinner.getSelectedItem();
		String minreward = minRewardEdit.getText().toString();
		String maxreward = maxRewardEdit.getText().toString();
		String status = (String) statusSpinner.getSelectedItem();
		String zip_code_filter = zip_codeEdit.getText().toString();
		String search_query = searchEdit.getText().toString();

		System.out.println(status);

		String query = "";

		if (!zip_code_filter.isEmpty()) {
			query += "zip_code=" + zip_code_filter + "&";
		}
		if (!search_query.isEmpty()) {
			query += "q=" + search_query + "&";
		}

		if (categorySpinner.getSelectedItemPosition() != 0) {
			query += "category=" + category + "&";
		}

		if (categorySpinner.getSelectedItemPosition() != 0) {
			query += "category=" + category + "&";
		}

		if (typeSpinner.getSelectedItemPosition() != 0) {
			query += "type=" + type + "&";
		}

		if (!choice.equals("Anytime")) {
			query += setDates(choice);
		}

		if (!minreward.equals(""))
			query += "minReward=" + Integer.valueOf(minreward) + "&";

		if (!maxreward.equals(""))
			query += "maxReward=" + Integer.valueOf(maxreward) + "&";

		if (!status.equals("Any"))
			query += "resolved="
					+ (status.equals("Resolved") ? "true" : "false") + "&";

		if (query.length() > 0 && query.charAt(query.length() - 1) == '&')
			query = query.substring(0, query.length() - 1);
		changePage.putExtra("DATABASEQUERY", query);
		startActivity(changePage);
	}

	/**
	 * Generates the correct query string for the specified date range
	 * 
	 * @param choice
	 *            The option of what the date range should be.
	 * @return A query string for min and max date.
	 */
	@SuppressLint("SimpleDateFormat")
	public String setDates(String choice) {
		Calendar rightnow = Calendar.getInstance();
		String[] opts = getActivity().getResources().getStringArray(
				R.array.DateEntries);
		if (choice.equals(opts[1]))
			rightnow.add(Calendar.DAY_OF_MONTH, -7);
		else if (choice.equals(opts[2]))
			rightnow.add(Calendar.MONTH, -1);
		else if (choice.equals(opts[3]))
			rightnow.add(Calendar.YEAR, -1);

		SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
		String ret = "minDate=" + ft.format(rightnow.getTime()) + "+00:00:00&";
		System.out.println(ret);
		return ret;
	}

}
